package com.jichangxiu.server.platform.action;

import cn.hutool.core.collection.ListUtil;
import com.jichangxiu.common.annotation.*;
import com.jichangxiu.common.entity.bo.PageData;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.framework.action.BaseAction;
import com.jichangxiu.server.platform.entity.vo.PlatformDeptAddVo;
import com.jichangxiu.server.platform.entity.vo.PlatformDeptEditVo;
import com.jichangxiu.server.platform.entity.vo.PlatformDeptQueryVo;
import com.jichangxiu.server.platform.service.PlatformDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;

/**
 * PlatformDeptAction:
 * 平台-部门管理控制器
 *
 * @author Jcx
 * @create 2023-02-25 22:10
 */
@Slf4j
@Rest("/platform/dept")
@Tag(name = "平台-部门管理控制器")
public class PlatformDeptAction extends BaseAction {

    @Resource
    private PlatformDeptService platformDeptService;

    /**
     * 新增部门
     *
     * @param platformDeptAddVo 部门视图对象
     * @return 是否成功
     */
    @Post("/add")
    @Operation(summary = "平台-新增部门")
    @PreAuthorize("@ss.hasPermission('platform:dept:add')")
    public String add(@RequestBody PlatformDeptAddVo platformDeptAddVo) {
        return toStr(platformDeptService.add(platformDeptAddVo));
    }

    /**
     * 多选级联删除部门
     *
     * @param deptIds 部门ID列表
     * @return 是否成功
     */
    @Delete("/del")
    @Operation(summary = "平台-多选级联删除部门")
    @PreAuthorize("@ss.hasPermission('platform:dept:del')")
    public String del(@RequestBody String[] deptIds) {
        return toStr(platformDeptService.del(ListUtil.toList(deptIds)));
    }

    /**
     * 修改部门信息
     *
     * @param platformDeptEditVo 部门视图对象
     * @return 是否成功
     */
    @Put("/edit")
    @Operation(summary = "平台-修改部门信息")
    @PreAuthorize("@ss.hasPermission('platform:dept:edit')")
    public String edit(@RequestBody PlatformDeptEditVo platformDeptEditVo) {
        return toStr(platformDeptService.edit(platformDeptEditVo));
    }

    /**
     * 根据ID获取树形部门详情
     *
     * @param deptId 部门ID
     * @return 树形部门详情
     */
    @Get("/info/{deptId}")
    @Operation(summary = "平台-根据ID获取树形部门详情")
    @PreAuthorize("@ss.hasPermission('platform:dept:info')")
    public Record info(@PathVariable("deptId") @NotBlank(message = "部门ID不能为空") String deptId) {
        return platformDeptService.info(deptId);
    }

    /**
     * 分页查询树形部门列表
     *
     * @param platformDeptQueryVo 部门视图对象
     * @return 分页树形部门列表
     */
    @Get("/pageList")
    @Operation(summary = "平台-分页查询树形部门列表")
    @PreAuthorize("@ss.hasPermission('platform:dept:pageList')")
    public PageData<Record> pageList(@ParameterObject PlatformDeptQueryVo platformDeptQueryVo) {
        return platformDeptService.pageList(platformDeptQueryVo);
    }

}
